{% extends "base_generic.html" %}

{% block title %}{{ author.first_name }} {{ author.last_name }} - Local Library{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h3 class="mb-0">{{ author.first_name }} {{ author.last_name }}</h3>
                    {% if perms.catalog.change_author %}
                        <div class="btn-group">
                            <a href="{% url 'author-update' author.pk %}" class="btn btn-outline-primary">
                                <i class="bi bi-pencil"></i> Edit
                            </a>
                            <a href="{% url 'author-delete' author.pk %}" class="btn btn-outline-danger">
                                <i class="bi bi-trash"></i> Delete
                            </a>
                        </div>
                    {% endif %}
                </div>
                <div class="card-body">
                    {% if author.date_of_birth %}
                        <div class="row mb-3">
                            <div class="col-sm-3">
                                <strong>Date of Birth:</strong>
                            </div>
                            <div class="col-sm-9">
                                {{ author.date_of_birth }}
                            </div>
                        </div>
                    {% endif %}

                    {% if author.date_of_death %}
                        <div class="row mb-3">
                            <div class="col-sm-3">
                                <strong>Date of Death:</strong>
                            </div>
                            <div class="col-sm-9">
                                {{ author.date_of_death }}
                            </div>
                        </div>
                    {% endif %}

                    <div class="row mb-3">
                        <div class="col-sm-3">
                            <strong>Books Written:</strong>
                        </div>
                        <div class="col-sm-9">
                            {{ author.book_set.count }}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-book"></i> Author Statistics</h5>
                </div>
                <div class="card-body text-center">
                    <div class="mb-3">
                        <h2 class="text-primary">{{ author.book_set.count }}</h2>
                        <p class="text-muted mb-0">Books Published</p>
                    </div>

                    {% if author.date_of_birth %}
                        <div class="mb-3">
                            {% now "Y" as current_year %}
                            {% if author.date_of_death %}
                                <h4 class="text-info">{{ author.date_of_death.year|add:"-"|add:author.date_of_birth.year }}</h4>
                                <p class="text-muted mb-0">Years Lived</p>
                            {% else %}
                                <h4 class="text-success">{{ current_year|add:"-"|add:author.date_of_birth.year }}</h4>
                                <p class="text-muted mb-0">Years Old</p>
                            {% endif %}
                        </div>
                    {% endif %}

                    <hr>
                    <p class="text-muted small">
                        {% if author.date_of_death %}
                            Legacy Author
                        {% else %}
                            Contemporary Author
                        {% endif %}
                    </p>
                </div>
            </div>
        </div>
    </div>

    <!-- Books by this Author -->
    <div class="mt-4">
        <div class="card">
            <div class="card-header">
                <h4><i class="bi bi-collection"></i> Books by {{ author.first_name }} {{ author.last_name }}</h4>
            </div>
            <div class="card-body">
                {% if author.book_set.all %}
                    <div class="row g-3">
                        {% for book in author.book_set.all %}
                            <div class="col-md-6 col-lg-4">
                                <div class="card h-100">
                                    <div class="card-body">
                                        <h6 class="card-title">
                                            <a href="{{ book.get_absolute_url }}" class="text-decoration-none">
                                                {{ book.title }}
                                            </a>
                                        </h6>
                                        <p class="card-text">
                                            <small class="text-muted">
                                                {{ book.summary|truncatewords:10 }}
                                            </small>
                                        </p>

                                        {% if book.publication_date %}
                                            <p class="card-text">
                                                <small class="text-muted">
                                                    <i class="bi bi-calendar"></i> {{ book.publication_date.year }}
                                                </small>
                                            </p>
                                        {% endif %}

                                        {% if book.genre.all %}
                                            <div class="mb-2">
                                                {% for genre in book.genre.all|slice:":3" %}
                                                    <span class="badge bg-secondary me-1">{{ genre.name }}</span>
                                                {% endfor %}
                                            </div>
                                        {% endif %}
                                    </div>
                                    <div class="card-footer bg-transparent">
                                        <a href="{{ book.get_absolute_url }}" class="btn btn-primary btn-sm w-100">
                                            View Book
                                        </a>
                                    </div>
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-book display-4 text-muted"></i>
                        <p class="mt-3 text-muted">No books by this author in our collection yet.</p>
                        {% if perms.catalog.add_book %}
                            <a href="{% url 'book-create' %}" class="btn btn-primary">
                                <i class="bi bi-plus"></i> Add Book
                            </a>
                        {% endif %}
                    </div>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="mt-4">
        <a href="{% url 'authors' %}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left"></i> Back to Authors
        </a>
    </div>
{% endblock %} 